/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package carreras.datos.connection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Fanky <fanky10@gmail.com>
 * v0.01 creacion de la clase los atributos y metodos
 * v0.02 se cierra mas la clase y maneja el cerrado de conexiones,apertura,etc
 * v0.03 singleton instance (fanky10) 2011-04-20
 * v0.04 singleton instance deprecated (fanky10) 2011-04-21
 * v0.05 limpiado de codigo, agregado de las properties a leer - fanky10 - 2011-07-12
 */
public abstract class DBConnector {
    public static final String PROP_SERVER="servidor";
    public static final String PROP_DATABASE="database";
    public static final String PROP_PORT="puerto";
    public static final String PROP_DB="db_object";
    
    public static final int DB_SQLITE=1;
    public static final int DB_POOLED=2;
    public static final int DB_MYSQL=3;
    
    protected Connection conn;
    protected Statements statementV;
    protected Statement statement;
    protected  ResultSet resultset;
    


    public static boolean DEBUG=true;

    protected static void debug(String text){
        if(DEBUG)
            System.out.println("DB-DEBUG: "+Thread.currentThread().getName()+" -- "+text);
    }
    
    //<editor-fold defaultstate="collapsed" desc="abstract-methods">
    public abstract void conectar() throws SQLException;

    public abstract ResultSet getResultSet(String query) throws SQLException ;

    protected abstract Statement getStmt() throws SQLException;

    public abstract void closeStmt() throws SQLException;

    public abstract void closeResultSet() throws SQLException;

    public abstract int executeUpdate(String non_query) throws SQLException;

    public abstract void closeConnection() throws SQLException ;

    public abstract boolean connectionIsClosed() throws SQLException ;

    public abstract void startTransaction() throws SQLException;

    public abstract void commitTransaction() throws SQLException;

    public abstract void rollbackTransaction() throws SQLException;

    public abstract void setAutoCommit(boolean aBool) throws SQLException;
    
    public abstract boolean isAutoCommit() throws SQLException;
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="static-util-methods">
    public static String adapta_string(String txt){
        return "\""+txt+"\"";
    }
    public static String traduce_boolean(boolean aBool){
        return aBool?"1":"0";
    }
    public static String traduce_nro(int nro){
        return String.valueOf(nro);
    }

    public static String traduce_nro(float nro){
        return String.valueOf(nro);
    }
    
    public static String traduce_nro(double nro){
        return String.valueOf(nro);
    }

    public static String traduce_nro(String nro_como_cadena){
        return traduce_nro(Float.parseFloat(nro_como_cadena.replace(",", ".")));
    }

    public static String traduce_nro(javax.swing.JTextField txt){
        return traduce_nro(txt.getText());
    }

    public static String traduce_nro(javax.swing.JCheckBox chk){
//        return chk.isSelected()?"1":"0";
        return traduce_boolean(chk.isSelected());
    }

    public static String traduce_time(java.sql.Time t){
        return "'"+new java.text.SimpleDateFormat("HH:mm:ss").format(t)+"'";
    }

    public static String traduce_timestamp(java.util.Date fecha){
        return traduce_date(fecha);
    }

    public static String traduce_date(javax.swing.JTextField txt){
        return traduce_date(txt.getText());
    }

    public static String traduce_date(java.util.Date fecha){
        return traduce_date(new java.text.SimpleDateFormat("dd/MM/yyyy").format(fecha));
    }

    public static String traduce_date(String cadena){
        char[] caracteres=cadena.toCharArray();
        return "'" + transforma_fecha(caracteres) + "'";
    }

    public static String transforma_fecha(char[] caracteres){
        return String.valueOf(caracteres[6])+String.valueOf(caracteres[7])+String.valueOf(caracteres[8])+String.valueOf(caracteres[9]) + "-" + String.valueOf(caracteres[3])+String.valueOf(caracteres[4]) + "-" + String.valueOf(caracteres[0])+String.valueOf(caracteres[1]);
    }

    public static String traduce_timestamp(java.sql.Timestamp ts){
        return "'" + transforma_fecha(new java.text.SimpleDateFormat("dd/MM/yyyy").format(ts).toCharArray()) + " " + new java.text.SimpleDateFormat("HH:mm:ss").format(ts) + "'";
    }

    public static String traduce_str(String cadena){
        return cadena==null?"null":"'" + cadena.replace("'", " ").replace("\r", " ").replace("\n", " ") + "'";
    }

    public static String traduce_str(javax.swing.JTextField txt){
        return "'" + txt.getText().replace("'", " ") + "'";
    }

    public static boolean es_numero(javax.swing.JTextField txt){
        return es_numero(txt.getText());
    }

    /*TODO: mejorar q funcione sin la excepcion*/
    public static boolean es_numero(String cadena){
        if(cadena.trim().equals("")){
            return false;
        }
        try{
            Float.parseFloat(cadena.replace(",", "."));
        }catch(Exception e){
            return false;
        }
        return true;
    }

    public static boolean es_fecha(javax.swing.JTextField txt){
        return es_fecha(txt.getText());
    }

    public static boolean es_fecha(String cadena){
        if(cadena.length()!=10){
            return false;
        }else{
            char[] caracteres=cadena.toCharArray();
            if(caracteres[2]!='/' || caracteres[5]!='/'){
                return false;
            }else if(
                    caracteres[0]<'0' || caracteres[0]>'9' ||
                    caracteres[1]<'0' || caracteres[1]>'9' ||
                    caracteres[3]<'0' || caracteres[3]>'9' ||
                    caracteres[4]<'0' || caracteres[4]>'9' ||
                    caracteres[6]<'0' || caracteres[6]>'9' ||
                    caracteres[7]<'0' || caracteres[7]>'9' ||
                    caracteres[8]<'0' || caracteres[8]>'9' ||
                    caracteres[9]<'0' || caracteres[9]>'9'
                    ){
                return false;
            }else if(Integer.valueOf(String.valueOf(caracteres[0])+String.valueOf(caracteres[1]))>31 || Integer.valueOf(String.valueOf(caracteres[3])+String.valueOf(caracteres[4]))>12 || Integer.valueOf(String.valueOf(caracteres[6])+String.valueOf(caracteres[7])+String.valueOf(caracteres[8])+String.valueOf(caracteres[9]))<2000 || Integer.valueOf(String.valueOf(caracteres[6])+String.valueOf(caracteres[7])+String.valueOf(caracteres[8])+String.valueOf(caracteres[9]))>2200){
                return false;
            }
        }
        return true;
    }

    public static String para_mostrar(float nro){
        return String.valueOf(nro).replace(".", ",");
    }
    //</editor-fold>
}
